//
//  XMGPublishViewController.m
//  百思不得姐
//
//  Created by 杨庆 on 2017/1/16.
//  Copyright © 2017年 杨庆. All rights reserved.
//

#import "XMGPublishViewController.h"
#import "XMGVerticalButton.h" //按钮: 图片在上,文字在下
 #import "POP.h"

@interface XMGPublishViewController ()
@property(nonatomic,strong)UIImageView*sloganView;
@end


@implementation XMGPublishViewController
static CGFloat const XMGSpringFactor = 10;
static CGFloat const XMGAnimationDelay = 0.1;
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// 界面不能点击
	[self.view setUserInteractionEnabled:NO];
	// 数据
	NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
	NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];

	// 2. 中间六个按钮
	int maxCols = 3;
	CGFloat buttonW = 72;
	CGFloat buttonH = buttonW + 30;
	CGFloat buttonStartX = 20;
	CGFloat buttonStartY =(XMGScreenH - 2*buttonH)* 0.5;
	CGFloat xMargin = (XMGScreenW - 2 *buttonStartX - maxCols*buttonW)/(maxCols-1);
	
	for (int i =0 ; i<images.count; i++) {
		XMGVerticalButton*button=[[XMGVerticalButton alloc]init];
		button.tag = i;
		[self.view addSubview:button];

		[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
		
		// 设置内容
		button.titleLabel.font = [UIFont systemFontOfSize:14];
		[button setTitle:titles[i] forState:UIControlStateNormal];
		[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		[button setImage:[UIImage imageNamed:images[i]]  forState:UIControlStateNormal];
		// 计算x\y
		int row = i / maxCols;
		int col = i % maxCols;
		
		CGFloat	buttonX = buttonStartX + col * (xMargin +buttonW);
		CGFloat	buttonEndY = buttonStartY + row * (buttonH +10) ;
		CGFloat	buttonBeginY = buttonEndY - XMGScreenH ;  // 超出屏幕,看不见状态
/**
 * 这里没有设置 frame  或 x 或 y
 * 下面的动画就是修改 frame
 * 1.pop 是真正修改了对象的属性
 * 2.Core Animation 仅仅修改表象, 不是真正修改 frame\size等值
 */
		// 添加动画
		POPSpringAnimation* anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
		anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonBeginY, buttonW, buttonH)];
		anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)];
		anim.springSpeed = XMGSpringFactor;
		anim.springBounciness = XMGSpringFactor;
		anim.beginTime = CACurrentMediaTime() + XMGAnimationDelay * i; // 0.1秒后执行下一个动画 : 延迟动画
		[button pop_addAnimation:anim forKey:nil];
		
	}
	// 标语
	UIImageView*sloganView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
	[self.view addSubview:sloganView];
	
	// 标语动画
	POPSpringAnimation*anim1=[POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
	CGFloat centerX = XMGScreenW * 0.5;
	CGFloat centerEndY = XMGScreenH * 0.2;
	CGFloat centerBeginY = centerEndY - XMGScreenH -200;
	anim1.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBeginY)];
	anim1.toValue   = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];
	anim1.beginTime = CACurrentMediaTime() + XMGAnimationDelay* images.count ;
	anim1.springSpeed = XMGSpringFactor;
	anim1.springBounciness = XMGSpringFactor;
	[anim1 setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
		self.view.userInteractionEnabled = YES;
	}];
	
	[sloganView pop_addAnimation:anim1 forKey:nil];

}



//  发布界面:按钮点击
// 调用方法:cancelWithCompletionBlock: 参数是一个 block
-(void)buttonClick:(UIButton*)button{

	[self cancelWithCompletionBlock:^{
		if (button.tag == 0) {
			XMGLog(@"发视频");
			
		}else if(button.tag ==1){
			XMGLog(@"发图片");
		}
	}];
}


// 推出modal 的控制器
- (IBAction)cancel:(id)sender {
	[self cancelWithCompletionBlock:nil];
	
//	[self dismissViewControllerAnimated:NO completion:nil];
}


/**
 *方法名: 参数(参数类型(block): 参数名) 
 */
-(void)cancelWithCompletionBlock:(void(^)())completionBlock{
	
	// 控制器的 view 不能点击
	self.view.userInteractionEnabled = NO;
    // XMGLog(@"%@",self.view.subviews);
	
	int beginIndex = 2;
	for(int i = beginIndex; i< self.view.subviews.count ; i++){
		//取出按钮
	UIView*subView = self.view.subviews[i];
		// 基本动画
		POPBasicAnimation*anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
		CGFloat centerY = XMGScreenH + subView.centerY;
		// 动画执行节奏: 一开始慢, 后面快
//		anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
		
		anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.centerX, centerY)];;
		anim.beginTime = CACurrentMediaTime() +(i - beginIndex)* XMGAnimationDelay;
		// 添加动画
		[subView pop_addAnimation:anim forKey:nil];
		
		// 监听最后一个动画
		if ( i == self.view.subviews.count - 1) {
			[anim setCompletionBlock:^(POPAnimation* anim, BOOL finished ) {
				// 推出Modal控制器
				[self dismissViewControllerAnimated:NO completion:nil];
				
				if (completionBlock) {
					completionBlock();
				}
				// !completionBlock ? : completionBlock();
			}];
		}
	}
}


// test 动画: 标题
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
	[self cancelWithCompletionBlock:nil];
}
@end
